/** setup gulp:
 npm install -g gulp
 npm install --save-dev gulp
 npm install --save-dev gulp-jshint
 **/

//基本使用方式：(1).保持原始模块:copyall -> umin -> zip  (2).使用r.js+ almond模块合并: rjs -> rmin -> zip
//模板构建：   (1). 生成tomod模块：修改tmod->tomod->tmove
//模块构建：   (1). 生成自定义模块：修改modules->app->appcopy

var gulp = require('gulp');
var gulpLoadPlugins = require("gulp-load-plugins");
var plugins = gulpLoadPlugins();
var path     = require("path");
var through  = require("through2");
var util     = require("gulp-util");
var pkg = require("./package.json");
var log4js = require("log4js");
var _ = require("underscore");
var debug = true;

/*开发路径定义，分开发目录和生产目录*/
var basePath = pkg.project;
var devPath = basePath + "/html";
var destPath = basePath + "/target";
var tempPath = destPath + "/temp";
var createLog =function(){
    return through.obj(function (file, enc, callback) {
        util.log(">>", util.colors.yellow(path.relative(process.cwd(), file.path)));
        callback(null, file);
    });
};

//构建app
var modules={
    app:"search",
    appDo:"searchDo",
    title:"搜索模块"
}

var tmod={
    name:"home"
};

//日志台控制
log4js.configure(pkg.log4js);
var logger = log4js.getLogger('cheese');
logger.setLevel('INFO');    // trace|debug|info|warn|error|fatal

/*
 * @desc  js压缩
 * @deps  none
 * @src   destPath/js/*.js
 * @dest  destPath/js
 */
gulp.task('jsmin', function () {
    return gulp.src(devPath + '/**/*.js')
        .pipe(plugins.uglify())
        //.pipe(plugins.concat("all.js"))
        //.pipe(plugins.rename({suffix: '.min'}))
        .pipe(gulp.dest(destPath));
});

/*
 * @desc  js语法检测
 * @deps  none
 * @src   none
 * @dest  none
 */
gulp.task('jshint', function () {
    return gulp.src([devPath + '/modules/**/*.js', devPath + '/vendor/api/**/*.js'])
        .pipe(plugins.jshint())
        .pipe(plugins.jshint.reporter('default'))
});

/*
 * @desc js less语言构建，将less转成css
 * @deps none
 * @src  devPath/less/*.less
 * @dest devPath/css
 */
gulp.task('less', function () {
    return gulp.src(devPath + '/less/*.less')
        .pipe(plugins.less())
        .pipe(plugins.autoprefixer('last 2 version', 'ie 8', 'ie 9'))
        .pipe(gulp.dest(devPath + '/css'));
});

/*
 * @desc css压缩
 * @src  devPath/css/*.css
 * @deps less
 * @dest destPath/css
 */
gulp.task("cssmin", function () {
    return gulp.src(destPath + '/css/*.css')
        .pipe(plugins.minifyCss({keepBreaks: true}))
        .pipe(gulp.dest(destPath + '/css'));
});

/*
 * @desc css构建&合并
 * @src  destPath/css
 * @deps cssmin
 * @dest destPath/css
 */
gulp.task("csscat", ['cssmin'], function () {
    return gulp.src([destPath + '/css/normalize.css',destPath + '/css/style.css',!destPath + '/css/plugins.css'])
        .pipe(plugins.concat("all.css"))
        .pipe(plugins.sourcemaps.init())
        .pipe(plugins.rename({suffix: '.min'}))
        .pipe(plugins.sourcemaps.write())
        .pipe(gulp.dest(destPath + '/css'))
});

/*
 * @desc httpServer服务启动-开发目录
 * @src  devPath
 * @deps none
 * @dest devPath
 * @host 0.0.0.0
 * @port 3000
 */
gulp.task("server", function () {
    gulp.src(devPath)
        .pipe(plugins.webserver({
            livereload: true,
            directoryListing: true,
            open: true,
            host: "0.0.0.0",
            fallback: 'index.html',
            port: "3000",
            enable: true
        }));
});

/*
 * @desc httpServer服务启动-生产目录
 * @src  devPath
 * @deps none
 * @dest devPath
 * @host 0.0.0.0
 * @port 3000
 */
gulp.task("online", function () {
    gulp.src(destPath)
        .pipe(plugins.webserver({
            livereload: true,
            directoryListing: true,
            open: true,
            host: "0.0.0.0",
            fallback: 'index.html',
            port: "3000",
            enable: true
        }));
});

/*
 * @desc  css,js引入资源自动嵌入（需要是真实存在的文件资源）
 * @deps  none
 * @src   devPath/*.html
 * @dest  destPath
 * @eg    <!--include:js(js/app/media.js)--> / <!-- include:css(css/allmin.css) -->
 */
gulp.task("include", function () {
    return gulp.src(devPath + '/*.html')
        .pipe(plugins.includeSource())
        .pipe(gulp.dest(tempPath));
});

/*
 * @desc 将所有资源项目中的block删除
 * @src  devPath
 * @deps none
 * @dest destPath
 * @demo<!--removeIf(production)--><!--endRemoveIf(production)-->
 */
gulp.task('xspace', ["include"], function () {
    return gulp.src(tempPath + '/*.html')
        .pipe(plugins.removeCode({production: true}))
        .pipe(gulp.dest(tempPath))
});

/*
 * @desc  js模板构建，页面写占位符，编译后生成页面
 * @deps  rjs
 * @src   devPath/*.html
 * @dest  destPath
 * @demo  <%=name%>
 */
gulp.task("template", ["xspace"], function () {
    return gulp.src(tempPath + '/*.html')
        //.pipe(plugins.template({name: 'jenking',age:27}))
        .pipe(plugins.data(function () {
            return {name: pkg.project, version: pkg.version, title: "shframework"};
        }))
        .pipe(plugins.template())
        .pipe(gulp.dest(tempPath))
        .pipe(createLog());
});

/*
 * @desc  css,js引入资源进行块范围替换
 * @deps  none
 * @src   destPath/*.html
 * @dest  destPath
 * @tag  <!-- build:css --><!-- endbuild -->
 */
gulp.task('replace',["template","csscat"],function () {
    return gulp.src(tempPath + '/*.html')
        .pipe(plugins.htmlReplace({
            css: {
                src: 'css/all.min',
                tpl: '<link href="%s.css" rel="stylesheet"/>'
            }
        }))
        .pipe(gulp.dest(tempPath));
});

/*
 * @desc  压缩html
 * @deps  replace
 * @src   destPath/*.html
 * @dest  destPath
 */
gulp.task("htmlmin", ["template","replace"], function () {
    return gulp.src(tempPath + '/*.html')
        .pipe(plugins.htmlmin({collapseWhitespace: true}))
        .pipe(gulp.dest(destPath));
});

/*
 * @desc  给css/js打上md5 tag
 * @deps  none
 * @src   destPath/*.html
 * @dest  destPath
 */
gulp.task("md5", function () {
    return gulp.src(devPath + "/css/*.css")
        .pipe(plugins.md5Plus(10, destPath + "/*.html"))
        .pipe(gulp.dest(destPath + "/css/"));
});

/*
 * @desc  拷贝资源css文件
 * @deps  none
 * @src   devPath
 * @dest  destPath
 */
gulp.task("copycss", function () {
    console.log("copy css....");
    return gulp.src(devPath + "/css/*.css")
        .pipe(gulp.dest(destPath + "/css"));
});

/*
 * @desc  拷贝资源js文件
 * @deps  none
 * @src   devPath
 * @dest  destPath
 */
gulp.task("copyjs", function () {
    console.log("copy js....");
    return gulp.src(devPath + "/js/*.js")
        .pipe(gulp.dest(destPath + "/js"));
});

/*
 * @desc  监听指定文件夹中内容变化，完成自动构建
 * @deps  none
 * @src   devPath
 * @dest  destPath
 */
gulp.task("watch", function (event) {
    if (debug) {
        gulp.watch(devPath + '/modules/**/*.html', ['xspace']);
        gulp.watch(devPath + '/less/**/*', ["less"]);
    }
});

gulp.watch([devPath + '/*.html', devPath + '/js/*.js', devPath + '/css/*.css']).on('change', function (event) {
    logger.info('Event type: ' + event.type);
    console.log('Event path: ' + event.path);
    plugins.livereload.changed
});

/*
 * @desc 清空构建目录
 * @src  destPath
 * @deps none
 * @dest destPath
 */
gulp.task("cleanTarget", function () {
    return gulp.src(destPath, {read: true})
        .pipe(plugins.clean());
});

gulp.task("copyall", ["copylib", "cleanTarget"], function () {
    return gulp.src(devPath + "/**/*").pipe(gulp.dest(destPath));
});


gulp.task("copylib", function () {
    return gulp.src(devPath + "/bridgeLib.js").pipe(gulp.dest(devPath + "/attach/js"));
});
/*
 * @desc 将缓存文件删除
 * @src  tempPath
 * @deps none
 * @dest destPath
 */
gulp.task('cleanTemp', function () {
    return gulp.src([tempPath, destPath + "/*.txt", destPath + "/less", destPath + "/build", basePath + "/bin",destPath + "/css/*.css",!destPath + "/css/all.min.css",])
        .pipe(plugins.clean());
});

/*
 * @desc 将所有资源项目用r.js进行压缩合并
 * @src  devPath
 * @deps cleanRjs
 * @dest destPath
 */
gulp.task('rjs', ["copylib", "cleanTarget"], function () {
    var spawn = require('child_process').spawn;
    var run = spawn('sh', ['-m', devPath + '/build/rjs.sh']);
    var start = +new Date();
    run.stdout.on('data', function (data) {
        console.log('stdout: ' + data);
    });
    run.stderr.on('data', function (data) {
        console.log('stderr: ' + data);
    });
    run.on('exit', function (code) {
        var end = +new Date();
        console.log("total:" + (end - start).toString() + "ms");
    });
});

/*
 * @desc 压缩html
 * @src  devPath
 * @deps rjs,replace,htmlmin
 * @dest devPath
 */
gulp.task('rhmin', ["htmlmin"], function () {
    console.log("html min");
});

/*
 * @desc 使用rjs进行代码合并
 * @src  devPath
 * @deps rjs,replace,htmlmin,zip
 * @dest devPath
 */
gulp.task('rmin', ['template'], function () {
    return gulp.src(tempPath + '/*.html')
        .pipe(gulp.dest(destPath))
        .pipe(plugins.notify({message: 'rmin project complete'}));
});

/*
 * @desc 编译html模板
 * @src  destPath
 * @deps none
 * @dest destPath
 */
gulp.task("parseTemp", function () {
    return gulp.src(destPath + '/*.html')
        .pipe(plugins.data(function () {
            return {name: 'shframework', version: "1.1", title: "output"};
        }))
        .pipe(plugins.template())
        .pipe(gulp.dest(destPath + "/modules"));
});

/*
 * @desc 使用uglifyjs进行代码合并
 * @src  devPath
 * @deps rjs,replace,htmlmin,zip
 * @dest devPath
 */
gulp.task('umin', ["jsmin", "cssmin", "parseTemp"], function () {
    console.log("umin ok");
});

/*
 * @desc 将所有资源项目打包压缩
 * @src  devPath
 * @deps cleanCss,cleanScript
 * @dest devPath
 */
gulp.task('zip', ["cleanTemp"], function () {
    return gulp.src(destPath + '/**/*')
        .pipe(plugins.zip("html.zip"))
        .pipe(gulp.dest(basePath + "/bin"))
        .pipe(createLog())
        .pipe(plugins.notify({message: 'coprass project complete'}));
});

/*
 * @desc 构建app
 * @src  devPath
 * @deps cleanCss,cleanScript
 * @dest devPath
 */
gulp.task('startapp',function () {
    return gulp.src([devPath + '/tmod/app/app.tpl',devPath + '/tmod/app/appDo.tpl',,devPath + '/tmod/app/app.html'])
       .pipe(plugins.fileInclude({
          prefix: '@@',
          basepath: '@file'
        }))
       .pipe(gulp.dest(devPath + '/tmod/app/dest'))
       .pipe(createLog());
});

gulp.task('app',["startapp"],function () {
    gulp.src([devPath + '/tmod/app/dest/app.tpl',devPath + '/tmod/app/dest/appDo.tpl',devPath + '/tmod/app/dest/app.html'])
        .pipe(plugins.data(function () {
            return {app: modules.app, appDo:modules.appDo,title:modules.title};
        }))
        .pipe(plugins.template())
        .pipe(gulp.dest(devPath + '/tmod/app/dest'))
        .pipe(createLog());
});

gulp.task('appcopy',function () {
    gulp.src(devPath + '/tmod/app/dest/app.tpl')
        .pipe(plugins.rename({
            basename: modules.app,
            extname: ".js"
        }))
        .pipe(gulp.dest(devPath + '/js/app/'+modules.app))
        .pipe(createLog());

    gulp.src(devPath + '/tmod/app/dest/appDo.tpl')
        .pipe(plugins.rename({
            basename: modules.app+"Do",
            extname: ".js"
        }))
        .pipe(gulp.dest(devPath + '/js/app/'+modules.app))
        .pipe(createLog());

    gulp.src([devPath + '/tmod/app/dest/*.html'])
        .pipe(plugins.rename({
            basename: modules.app,
            extname: ".html"
        }))
        .pipe(gulp.dest(devPath))
        .pipe(createLog());
});

/*
 * @desc 构建依赖库
 * @src  devPath
 * @deps cleanCss,cleanScript
 * @dest devPath
 */
gulp.task('bower', function () {
    var path=[];
    _.each(pkg.libs,function(v,i){
        if(v.path){
            path.push(v.path);
        }else{
            return true;
        }
    });
    return gulp.src(path)
        .pipe(gulp.dest(devPath+"/js/lib"));
});

/*
 * @desc  编译tomod
 * @deps  none
 * @src   dev/template/*.html
 * @dest  destPath
 */
gulp.task("tomod", function () {
    var module_tpl = tmod.name;
    return gulp.src(devPath + '/tmod/' + module_tpl + '/dev/*.html')
        .pipe(plugins.tmod({
            base: devPath + '/tmod/' + module_tpl + '/dev',
            combo: true,
            minify: true,
            cache: true,
            type: "default",    //default、cmd、amd、commonjs
            helpers: devPath + '/tmod/' + module_tpl + '/dev/helpers/template-helper.js',
            output: devPath + '/tmod/' + module_tpl
        }));
});

gulp.task("tmove", ["tomod"], function () {
    var module_tpl = tmod.name;
    return gulp.src(devPath + '/tmod/' + module_tpl+'/template.js')
        .pipe(gulp.dest(devPath + "/js/app/home"));
});